import type { LabelValueState } from '@/types/global'
import http from '@/utils/http'
import type * as T from './type'

export type * from './type'

const BASE_URL = '/website_management/category'

export interface CategoryResp {
  id: string
  name: string
  description: string
  pid: string
  sort: string
  status: string
  createUserString: string
  updateUserString: string
}
export interface CategoryDetailResp {
  id: string
  name: string
  description: string
  pid: string
  sort: string
  status: string
  createUser: string
  createTime: string
  updateUser: string
  updateTime: string
  createUserString: string
  updateUserString: string
}
export interface CategoryQuery {
  name: string
  // status: string
  // sort: Array<string>
}
export interface CategoryPageQuery extends CategoryQuery, PageQuery { }

export interface CategoryTreeQuery {
  name?: string
  status?: number
  parentId?: number
}

/** @desc 查询分类树 */
export function listCategory(query: CategoryTreeQuery) {
  return http.get<T.CategoryTreeResp[]>(`${BASE_URL}/tree`, query)
}

/** @desc 查询分类详情 */
export function getCategory(id: string) {
  return http.get<CategoryDetailResp>(`${BASE_URL}/${id}`)
}

/** @desc 查询分类列表 */
export function listCategoryDict(query: CategoryTreeQuery) {
  return http.get<LabelValueState[]>(`${BASE_URL}/tree`, query)
}

/** @desc 新增分类 */
export function addCategory(data: any) {
  return http.post(`${BASE_URL}`, data)
}

/** @desc 修改分类 */
export function updateCategory(data: any, id: string) {
  return http.put(`${BASE_URL}/${id}`, data)
}

/** @desc 删除分类 */
export function deleteCategory(id: string) {
  return http.del(`${BASE_URL}/${id}`)
}

/** @desc 导出分类 */
export function exportCategory(query: CategoryQuery) {
  return http.download<any>(`${BASE_URL}/export`, query)
}

/** @desc 获取顶级分类列表 */
export function useCategoryDict() {
  const categoryList = ref<LabelValueState[]>([])
  const categoryQuery: CategoryTreeQuery = { parentId: 0 }

  const getCategoryList = async () => {
    const res = await listCategoryDict(categoryQuery)
    categoryList.value = res.data
  }

  return { categoryList, getCategoryList }
}


